package com.example.transaction_springboot.controller;

import cn.hutool.core.io.FileUtil;
import com.example.transaction_springboot.common.Result;
import com.example.transaction_springboot.controller.request.BookRequest;
import com.example.transaction_springboot.entity.Book;
import com.example.transaction_springboot.interceptor.AuthAccess;
import com.example.transaction_springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RequestMapping("/book")
@RestController
public class BookController {
    //配置相关根路径，获取的是配置文件中的信息

    @Value("${ip:localhost}")
    String ip;

    @Value("${server.port}")
    String port;

    private static final String ROOT_PATH = System.getProperty("user.dir") + File.separator + "file";

    @Autowired
    private BookService bookService;

    @GetMapping("/list")
    public Result list() {
        return Result.success(bookService.list());
    }


    @GetMapping("/selectByPage")
    public Result selectByPage(BookRequest bookRequest) {
        return Result.success(bookService.page(bookRequest));
    }

    @PostMapping("/addBook")
    public Result addUser(@RequestBody Book book) {
        bookService.addBook(book);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Book book = bookService.selectById(id);
        return Result.success(book);
    }

    @PostMapping("/update")
    public Result updateById(@RequestBody Book book) {
        bookService.updateById(book);
        return Result.success();
    }

    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        bookService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename(); //获取文件的原始名称
        String mainName = FileUtil.mainName(originalFilename);
        String extName = FileUtil.extName(originalFilename);
        if (!FileUtil.exist(ROOT_PATH)){
            FileUtil.mkdir(ROOT_PATH); //父级文件目录不存在，则创建
        }
        //如果当前上传文件已经存在，则重命名一个文件名
        if (FileUtil.exist(ROOT_PATH + File.separator + originalFilename)){
            originalFilename = System.currentTimeMillis() + "_" +
                    mainName + "." + extName;
        }

        //保存到本地
        file.transferTo(new File(ROOT_PATH + File.separator + originalFilename));
        //拼接url
        String url = "http://" + ip + ":" + port + "/book/download/" + originalFilename;
        return Result.success(url);
    }

    @AuthAccess
    @GetMapping("/download/{fileName}")
    public void download(@PathVariable String fileName, HttpServletResponse response) throws IOException{
        String filePath = ROOT_PATH + File.separator + fileName;
        if (!FileUtil.exist(filePath)){
            return;
        }
        byte[] bytes = FileUtil.readBytes(filePath);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }


}
